package com.xinghou.platform.lucene.main.util;

import com.xinghou.platform.page.Pagination;
import com.xinghou.platform.util.log.LoggerFactory;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.FacetField;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.slf4j.Logger;

import java.io.IOException;
import java.util.List;

public class SolrJOper<T> {
    private Logger logInfo = LoggerFactory.getServerInfoLogger(SolrJOper.class);
    /**
     * 根据关键字查询
     *
     * @param <T>
     * @param keyword  搜索关键字
     * @param pageSize 每页显示的大小
     * @param clzz     对象类型
     * @return
     */
    public <T> Pagination<T> queryBean(SolrClient server, String keyword, int fromRow, int pageSize, Class<T> clzz) {
        SolrQuery query = new SolrQuery();
        query.setQuery(keyword);
        query.set("shards.tolerant",true);
        query.set("timeAllowed","4000");
        query.setStart(fromRow);
        query.setRows(pageSize);
        QueryResponse response = null;
        try {
            response = server.query(query);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        //查询到的记录总数
        long totalRow = Long.valueOf(response.getResults().getNumFound()).intValue();
        //查询结果集
        List<T> items = response.getBeans(clzz);
        //填充page对象
        int pageNum = fromRow / pageSize + 1;
        return new Pagination<T>(pageNum, pageSize, items);
    }


    public <T> Pagination<T> queryLbsBean(SolrClient server, String keyword, Double distince, String sfield, String position, int fromno, int pageSize, Class<T> clzz) {
        SolrQuery query = new SolrQuery();
        query.setQuery(keyword);
        query.setStart(fromno);
        query.setRows(pageSize);
        query.addFilterQuery("{!geofilt}");
        query.set("sort", "geodist() asc");
        query.set("d", distince.toString());
        query.set("sfield", sfield);
        query.set("pt", position);
        query.set("shards.tolerant",true);
        query.set("timeAllowed","4000");
        logInfo.info("keyword=" + keyword + ",fromno=" + fromno + ",pageSize=" + pageSize + ",d=" + distince.toString()+",sfield=" + sfield + ",pt=" + position + ",sort=eodist() asc,fq={!geofilt}");
        QueryResponse response = null;
        try {
            response = server.query(query);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        //查询到的记录总数
        long totalRow = Long.valueOf(response.getResults().getNumFound()).intValue();
        //查询结果集
        SolrDocumentList list = response.getResults();
        List<T> items = response.getBeans(clzz);
        //填充page对象
        int pageNum = fromno / pageSize + 1;
        return new Pagination<T>(pageNum, pageSize, items);
    }

    public FacetField queryCount(SolrClient server, String keyword, Double distince, String sfield, String position,Double level) {
        SolrQuery query = new SolrQuery();
        query.setQuery(keyword);
        query.addFilterQuery("{!geofilt}");
        query.set("sort", "geodist() asc");
        query.set("d", distince.toString());
        query.set("sfield", sfield);
        query.set("pt", position);
        query.set("shards.tolerant",true);
        query.set("timeAllowed","4000");
        if(level!=null){
            if(level>0&&level<4.5){
                query.addFacetField("ad_code_province");
            }else if(level>=4.5&&level<8.5){
                query.addFacetField("ad_code_city");
            }else if(level>=8.5&&level<12.5){
                query.addFacetField("ad_code_region");
            }
        }
        query.addFacetField("ad_code");
        logInfo.info("keyword=" + keyword +  ",d=" + distince.toString()+",sfield=" + sfield + ",pt=" + position + ",sort=eodist() asc,fq={!geofilt}");
        QueryResponse response = null;
        try {
            response = server.query(query);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        FacetField facetField =response.getFacetFields().get(0);

        return facetField;
    }
    /**
     * 根据关键字查询 [测试通过 - 使用 solr内部转换机制]
     *
     * @param <T>
     * @param keyword  搜索关键字
     * @param pageNum  当前页码
     * @param pageSize 每页显示的大小
     * @param clzz     对象类型
     * @return
     */
    public <T> Pagination<T> queryBinderBean(SolrClient server, String keyword, int pageNum, int pageSize, Class<T> clzz) {
        SolrQuery query = new SolrQuery();
        query.setQuery(keyword);
        query.setStart((pageNum - 1) * pageSize);
        query.setRows(pageSize);
        query.set("shards.tolerant",true);
        query.set("timeAllowed","4000");
        QueryResponse response = null;
        try {
            response = server.query(query);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        //查询结果集
        SolrDocumentList documents = response.getResults();

        //使用DocumentObjectBinder获取
        List<T> items = server.getBinder().getBeans(clzz, documents);

        //查询到的记录总数
        long totalRow = Long.valueOf(response.getResults().getNumFound()).intValue();
        //填充page对象
        return new Pagination<T>(pageNum, pageSize, items);
    }




    public void addBean(SolrClient solrServer, T o) {
        try {
            solrServer.addBean(o);
            solrServer.commit();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SolrServerException e) {
            e.printStackTrace();
        }

    }


}  
